package com.xy.util;

import android.content.Context;
import android.os.Looper;
import android.text.TextUtils;
import android.widget.Toast;

/**
 * Created by XuYang
 * 2023/4/21
 * Email:544066591@qq.com
 */
public class ToastUtil {

    public static void showShort(Context ctx, String msg) {
        if (null == ctx || TextUtils.isEmpty(msg)) {
            return;
        }
        if (Looper.myLooper() == Looper.getMainLooper()) {
            Toast.makeText(ctx, msg, Toast.LENGTH_SHORT).show();
        } else {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    Looper.prepare();
                    Toast.makeText(ctx, msg, Toast.LENGTH_SHORT).show();
                    Looper.loop();
                }
            }).start();
        }
    }

    public static void showLong(Context ctx, String msg) {
        if (null == ctx || TextUtils.isEmpty(msg)) {
            return;
        }
        if (Looper.myLooper() == Looper.getMainLooper()) {
            Toast.makeText(ctx, msg, Toast.LENGTH_LONG).show();
        } else {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    Looper.prepare();
                    Toast.makeText(ctx, msg, Toast.LENGTH_LONG).show();
                    Looper.loop();
                }
            }).start();
        }
    }
}
